package com.slj.config;

import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.Configuration;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

@Component
public class MybatisConfig {
    @Bean
    public SqlSessionFactoryBean SqlSessionFactoryBean(DataSource dataSource) throws Exception {
        //创建SqlSession工厂
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        //设置数据源
        sqlSessionFactoryBean.setDataSource(dataSource);
        //设置日志 ibatis的日志
        Configuration configuration = new Configuration();
        configuration.setLogImpl(StdOutImpl.class);
        //开启全局二级缓存
        configuration.setCacheEnabled(true);
        //加载进去
        sqlSessionFactoryBean.setConfiguration(configuration);
        //返回工厂对象
        return sqlSessionFactoryBean;

    }
    @Bean
    public MapperScannerConfigurer MapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.slj.mapper");
        return mapperScannerConfigurer;
    }
}
